const express = require('express');
const router = express.Router();
const { pool } = require('../config/database');
const { authMiddleware, adminMiddleware } = require('./auth');
const dayjs = require("dayjs")

// 获取所有客服成员的信息
router.get('/server_list', async (req, res) => {
    const { id } = req.query
    try {
        let sql = `
            SELECT 
                u.id, 
                u.role, 
                u.username, 
                u.phone,
                COUNT(m.id) AS message_count
            FROM 
                users u
            LEFT JOIN 
                messages m ON u.id = m.sender_id AND m.receiver_id = ? AND m.read_at IS NULL
            WHERE 
                u.role IN ('customer_service', 'technical_support')
            GROUP BY
                u.id, u.role, u.username, u.phone
        `
        const [results] = await pool.query(sql, [id])
        return res.status(200).json({
            code: 0,
            msg: "获取所有客服的成员的信息成功",
            data: results
        })
    } catch (error) {
        return res.status(500).json({
            code: -1,
            msg: "获取所有客服的成员的信息失败",
            data: {
                error: error
            }
        })
    }
})

// 获取所有跟客服对话的用户的信息列表
router.get('/user_list', async (req, res) => {
    const { id } = req.query
    try {
        let sql = `
            SELECT 
                u.id, 
                u.role, 
                u.username, 
                u.phone,
                COUNT(m.id) AS message_count
            FROM 
                users u
            LEFT JOIN 
                messages m ON u.id = m.sender_id AND m.receiver_id = ? AND m.read_at IS NULL
            WHERE 
                u.role NOT IN ('customer_service', 'technical_support')
            GROUP BY
                u.id, u.role, u.username, u.phone
        `
        const [results] = await pool.query(sql, [id])
        return res.status(200).json({
            code: 0,
            msg: "获取客服对话的用户信息成功",
            data: results
        })
    } catch (error) {
        return res.status(500).json({
            code: -1,
            msg: "获取客服对话的用户信息失败",
            data: {
                error: error
            }
        })
    }
})

// 获取用户发来的信息
router.get('/msg_list', async (req, res) => {
    const { receiver_id, user_id } = req.query
    try {
        let sql = `
            SELECT 
                m.receiver_id as receiverId,
                m.sender_id as senderId,
                m.content,
                m.created_at
            FROM 
                messages m
            WHERE 
                m.system = 'false' AND (m.receiver_id = ? AND m.sender_id = ?) OR 
				(m.receiver_id = ? AND m.sender_id = ?)
        `
        const [results] = await pool.query(sql, [receiver_id, user_id, user_id, receiver_id])
        return res.status(200).json({
            code: 0,
            msg: "获取客服对话的用户信息成功",
            data: results
        })
    } catch (error) {
        return res.status(500).json({
            code: -1,
            msg: "获取客服对话的用户信息失败",
            data: {
                error: error
            }
        })
    }
})

// 已读消息
router.post('/set_msg', async (req, res) => {
    const { server_id, user_id } = req.body
    console.log("req.body————————", req.body)
    const newTime = dayjs(new Date()).format("YYYY-MM-DD HH:mm:ss")
    try {
        let sql = `
            UPDATE messages m SET m.read_at = ?
            WHERE 
                m.read_at IS NULL AND m.receiver_id = ? AND m.sender_id = ?
        `
        const [results] = await pool.query(sql, [newTime, Number(user_id), server_id])
        return res.status(200).json({
            code: 0,
            msg: "已读消息设置成功",
            data: results
        })
    } catch (error) {
        return res.status(500).json({
            code: -1,
            msg: "已读消息设置失败",
            data: {
                error: error
            }
        })
    }
})
module.exports = router;